package be.dch.gui.panels;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import be.dch.gui.components.JImagePanel;
import be.dch.typeditor.domain.GarminIcon;
import be.dch.typeditor.domain.Language;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class TypEditorDetailPanel extends JPanel {

    private static final long serialVersionUID = -8942652706702850772L;
    private static final String COLUMN_SPECS = "3dlu,pref,3dlu,pref:grow,3dlu,pref,3dlu,pref,3dlu,pref,3dlu";
    private static final String ROW_SPECS = "3dlu,pref,3dlu,pref,3dlu,pref,3dlu,pref,3dlu,pref,3dlu,pref:grow";

    private final transient JTextField txtName;
    private transient GarminIcon garminIcon;
    private final transient JImagePanel imgPanel;

    public TypEditorDetailPanel() {
	super();
	final FormLayout layout = new FormLayout(TypEditorDetailPanel.COLUMN_SPECS, TypEditorDetailPanel.ROW_SPECS);
	final CellConstraints constraints = new CellConstraints();
	final PanelBuilder builder = new PanelBuilder(layout);

	final JLabel lblName = new JLabel("Name");
	builder.add(lblName, constraints.rchw(2, 2, 1, 1));

	this.txtName = new JTextField();
	lblName.setLabelFor(this.txtName);
	builder.add(this.txtName, constraints.rchw(2, 4, 1, 1));

	final JComboBox cmbLanguage = new JComboBox(Language.getValues());
	builder.add(cmbLanguage, constraints.rchw(2, 6, 1, 1));

	this.imgPanel = new JImagePanel();
	builder.add(this.imgPanel, constraints.rchw(4, 2, 1, 1));

	this.add(builder.getPanel());
    }

    public void setGarminIcon(final GarminIcon garminIcon) {
	this.garminIcon = garminIcon;
	this.updateComponents();
    }

    private void updateComponents() {
	if (this.garminIcon == null) {
	    this.imgPanel.noImage();
	    this.txtName.setText(null);
	} else {
	    this.imgPanel.setImage(this.garminIcon.getImage());
	    this.txtName.setText(this.garminIcon.getLabel());
	}
    }
}
